//
//  KZAnswerClassSelectingView.m
//  lg_zhinengbeikao_english
//
//  Created by 朱凯 on 2019/1/10.
//  Copyright © 2019 zhukai. All rights reserved.
//

#import "KZAnswerClassSelectingView.h"

#import "KZOptionPanelView.h"

@implementation KZAnswerClassSelectingView
{
    
    int _scrollDirection;               // 滑动方向
    CGPoint _scrollViewStartPosPoint;   // 开始拖动滑动视图时，偏移的点，用于计算是往哪个方向滑动
}

- (instancetype)init {
    if (self = [super init]) {
        self.delegate = self;
        self.pagingEnabled = true;
    }
    return self;
}

#pragma mark - 视图
- (void)setupUI {
    
    UIView *last = nil;
    for (int i = 0; i < _model.optionsList.count; i ++) {
        NSDictionary *options = _model.optionsList[i];
        KZOptionPanelView *optionPanelView = [KZOptionPanelView new];
        optionPanelView.options = options;
        [self addSubview:optionPanelView];
        [optionPanelView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.equalTo(self);
            make.top.mas_equalTo(H(10));
            if (last) {
                make.left.equalTo(last.mas_right);
            } else {
                make.left.mas_equalTo(0);
            }
        }];
        last = optionPanelView;
    }
    
    [last mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(H(-10));
        make.right.mas_equalTo(0);
    }];
}

#pragma mark - UIScrollViewDelegate
#pragma mark - 控制滑动视图同时只能往一个方向滑动
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (_scrollDirection == 0){
        if (fabs(_scrollViewStartPosPoint.x - scrollView.contentOffset.x)<
            fabs(_scrollViewStartPosPoint.y - scrollView.contentOffset.y)){
            _scrollDirection = 1;
        } else {
            _scrollDirection = 2;
        }
    }
    if (_scrollDirection == 1) {
        
        scrollView.contentOffset = CGPointMake(_scrollViewStartPosPoint.x,scrollView.contentOffset.y);
    } else if (_scrollDirection == 2){
        scrollView.contentOffset = CGPointMake(scrollView.contentOffset.x,_scrollViewStartPosPoint.y);
    }
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    _scrollViewStartPosPoint = scrollView.contentOffset;
    _scrollDirection = 0;
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    if (decelerate) {
        _scrollDirection =0;
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    _scrollDirection =0;
}

#pragma mark - setter
- (void)setModel:(KZExamItemModel *)model {
    
    _model = model;
    [self setupUI];
}
@end
